package com.loto.insurance.oem.web.service;

import java.math.BigDecimal;
import java.util.Calendar;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.loto.insurance.oem.web.db.mapper.CustFeeFlowMapper;
import com.loto.insurance.oem.web.db.mapper.CustInfoMapper;
import com.loto.insurance.oem.web.entity.CustFeeFlow;
import com.loto.insurance.oem.web.entity.CustFeeInit;
import com.loto.insurance.oem.web.entity.CustInfo;
import com.loto.insurance.oem.web.enumeration.FeeTypeEnum;
import com.loto.insurance.oem.web.enumeration.PaymentStatusEnum;

@Service
public class CustFeeFlowService {

  @Autowired
  CustFeeFlowMapper custFeeFlowMapper;

  @Autowired
  CustInfoMapper custInfoMapper;

  public List<CustFeeFlow> findFlowByPayNo(String flowPayNo) {
	return custFeeFlowMapper.findFlowByPayNo(flowPayNo);
  }

  public long insertCustFeeFlow(CustFeeFlow custFeeFlow) {
	return custFeeFlowMapper.insertCustFeeFlow(custFeeFlow);
  }

  public boolean insertRoyaltyFeeFlow(CustFeeInit feeInit) {
	BigDecimal fee = feeInit.getInitRoyaltyFee();
	if (fee.compareTo(new BigDecimal(0)) > 0) {
	  CustInfo custInfo = custInfoMapper.findUsefulfeeByName("admin");
	  BigDecimal usefulFee = custInfo.getCustUsefulFee();
	  CustFeeFlow feeFlow = new CustFeeFlow();
	  feeFlow.setFlowAfterFee(usefulFee.add(fee));
	  feeFlow.setFlowBeforeFee(usefulFee);
	  feeFlow.setFlowChangedFee(fee);
	  feeFlow.setFlowChannel(feeInit.getInitChannel());
	  Calendar now = Calendar.getInstance();
	  feeFlow.setFlowCreditedOn(now.getTime());
	  feeFlow.setFlowCustFid(custInfo.getCustId());
	  feeFlow.setFlowFlag(PaymentStatusEnum.SUC.getIndex());
	  feeFlow.setFlowFor(feeInit.getInitFor());
	  feeFlow.setFlowMemo(feeInit.getInitRoyaltyMemo());
	  feeFlow.setFlowOutPayNo(feeInit.getInitOutPayNo());
	  feeFlow.setFlowPayNo(feeInit.getInitOutPayNo());
	  feeFlow.setFlowRelatedFid(feeInit.getInitRelatedFid());
	  feeFlow.setFlowType(FeeTypeEnum.ROYALTY.getIndex());
	  if (custFeeFlowMapper.insertCustFeeFlow(feeFlow) > 0)
		return true;
	  else
		return false;
	} else
	  return true;
  }

  public long findFlowByCustCount(Map<String, Object> params) {
	return custFeeFlowMapper.findFlowByCustPagerCount(params);
  }

  public List<CustFeeFlow> findFlowByCust(Map<String, Object> params) {
	return custFeeFlowMapper.findFlowByCustPager(params);
  }

  public long findFlowCount(Map<String, Object> params) {
	return custFeeFlowMapper.findFlowPagerCount(params);
  }

  public List<CustFeeFlow> findFlow(Map<String, Object> params) {
	return custFeeFlowMapper.findFlowPager(params);
  }
}
